package com.ftf.tp.user.application.service.impl;


import com.ftf.tp.common.constants.ExperienceConstant;
import com.ftf.tp.common.core.exception.BusinessException;
import com.ftf.tp.common.core.utils.ConverterUtil;
import com.ftf.tp.user.application.adapter.http.PassportRestAdaptor;
import com.ftf.tp.user.application.dto.ArticleAddDTO;
import com.ftf.tp.user.application.message.BlogAddEvent;
import com.ftf.tp.user.application.service.UserApplicationService;
import com.ftf.tp.user.application.vo.AuthorVO;
import com.ftf.tp.user.domain.data.dos.TpUserDO;
import com.ftf.tp.user.domain.data.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 用户service实现类
 *
 * @author tanghao
 * @date 2021/11/11
 */
@Service
@Slf4j
public class UserApplicationServiceImpl implements UserApplicationService {

    /**
     * 用户仓储
     */
    @Resource
    private UserRepository userRepository;

    /**
     * 获取当前登陆的用户信息
     *
     * @return 当前登陆用户信息
     */
    @Override
    public TpUserDO getUserInfo() {
        return userRepository.getUserInfo();
    }

    /**
     * 新增文章相关用户操作
     *
     * @param articleAddDTO 新建文章参数
     */
    @Override
    public void blogAddAboutUser(ArticleAddDTO articleAddDTO) {
        log.info("发布文章用户服务操作,{}", articleAddDTO);
        Long articleAuthorId = articleAddDTO.getArticleAuthorId();
        //查出作者的所有关注者id
        List<Long> followerList = userRepository.selectFollowerList(articleAuthorId);
        //组装给关注者的消息内容
        String message = "你关注的作者 " + articleAddDTO.getArticleAuthorName() + " 发布了一篇新文章 " + articleAddDTO.getArticleTitle()
                + " 快来围观吧!";
        BlogAddEvent blogAddEvent = new BlogAddEvent(message, articleAddDTO.getArticleId(), followerList);
        addExperience(articleAuthorId, ExperienceConstant.PUBLISH_BLOG_EXPERIENCE);
        //发送发布文章消息给关注者
//        blogAddPublish.publishContractLog(blogAddEvent);
    }

    /**
     * 增加经验值
     *
     * @param userId     用户id
     * @param experience 需要增加的经验值
     */
    public void addExperience(Long userId, Integer experience) {
        TpUserDO tpUserDO = userRepository.byId(userId);
        Optional.ofNullable(tpUserDO).orElseThrow(() -> new BusinessException("用户id有误,没有对应的用户信息"));
        int now = tpUserDO.getLevelExperience() + experience;
        Integer level = tpUserDO.getLevel();
        //每升一级需要的经验值为等级*100
        if (now > level * ExperienceConstant.LEVEL_EXPERIENCE) {
            //满足升级经验 等级+1
            level = level + 1;
            now = now - level * ExperienceConstant.LEVEL_EXPERIENCE;
        }
        tpUserDO.setLevelExperience(now);
        tpUserDO.setLevel(level);
        userRepository.update(tpUserDO);
    }


    /**
     * 根据id获取作者信息
     *
     * @param authorId 作者id
     */
    @Override
    public AuthorVO getUserInfoByUserId(Long authorId) {
        AuthorVO authorVO = new AuthorVO();
        TpUserDO tpUserDO = userRepository.byId(authorId);
        ConverterUtil.copyPropertiesIgnoreNull(tpUserDO, authorVO);
        return authorVO;
    }
}
